package com.nexus.core.service.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;

import java.beans.Transient;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 所有实体类的父类（含id、createTime等公共字段）
 * 基本数据结构
 *
 * @Date 2025/7/23 14:20
 * @Author luzhengning
 **/
@Data
public abstract class BaseEntity extends Model<BaseEntity> implements Serializable {

    /**
     * 主键
     *
     * @Date 2025/7/23 15:04
     * @Author luzhengning
     **/
    @TableId
    protected String id;

    /**
     * 创建人
     *
     * @Date 2025/7/23 15:05
     * @Author luzhengning
     **/
    @TableField(fill = FieldFill.INSERT)
    protected String createBy;

    /**
     * 创建时间
     *
     * @Date 2025/7/23 15:04
     * @Author luzhengning
     **/
    @TableField(fill = FieldFill.INSERT)
    protected LocalDateTime createTime;

    /**
     * 修改人
     *
     * @Date 2025/7/23 15:06
     * @Author luzhengning
     **/
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected String updateBy;

    /**
     * 修改时间
     *
     * @Date 2025/7/23 15:06
     * @Author luzhengning
     **/
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected LocalDateTime updateTime;

    /**
     * 删除标识
     * 逻辑删除
     *
     * @Date 2025/7/23 15:07
     * @Author luzhengning
     **/
    @TableLogic
    @TableField(fill = FieldFill.INSERT)
    protected Integer deleted;

    /**
     * 乐观锁
     *
     * @Date 2025/7/23 15:07
     * @Author luzhengning
     **/
    @Version
    protected Integer version;

    /**
     * 备注
     *
     * @Date 2025/7/23 15:08
     * @Author luzhengning
     **/
    protected String remark;

    /**
     * 设置更新创建时间
     *
     * @Date 2025/7/23 15:11
     * @Author luzhengning
     **/
    protected void onCreateTime() {
        this.createTime = LocalDateTime.now();
    }

    /**
     * 设置更新修改时间
     *
     * @Date 2025/7/23 15:11
     * @Author luzhengning
     **/
    protected void onUpdateTime() {
        this.updateTime = LocalDateTime.now();
    }

    /**
     * 是否是新数据
     *
     * @Date 2025/7/23 15:14
     * @Author luzhengning
     **/
    @Transient
    protected boolean isNew() {
        return this.id == null || this.id.trim().length() == 0;
    }
}
